﻿/*创建人：盛金龙
 *时间：2011/12/28 周三 14:52:43.01
 *用途：添加用户
 */
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using EnvironmentManagerSystem.MODE;

namespace EnvironmentManagerSystem.UserManager
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {
                clear();
                BingList();
            }
        }

        /// <summary>
        /// 清空数据方法
        /// </summary>
        public void clear()
        {
            txtName.Text = "";
            txtLogName.Text = "";
            txtPwd.Text = "";
            txtName.Text = "";
            txtType.Text = "";
            cbo.Checked = true;
            dlstReold.SelectedIndex = 0;
            txtName.Focus();
        }

        /// <summary>
        /// 绑定角色数据表
        /// </summary>
        public void BingList()
        {
            DataTable dt = EnvironmentManagerSystem.BLL.User.GetRole();
            dlstReold.DataSource = dt;
            dlstReold.DataTextField = "Name";
            dlstReold.DataValueField = "id";
            dlstReold.DataBind();
        }

        //重置事件
        protected void btnClear_Click(object sender, EventArgs e)
        {
            clear();
        }

        //确定事件
        protected void btnOk_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                try
                {
                    UserData use = new UserData();
                    use.UserName = txtName.Text.Trim();
                    use.LoginName = txtLogName.Text.Trim();
                    use.Password = txtPwd.Text.Trim();
                    use.Phone = txtTel.Text.Trim();
                    use.RoleId = dlstReold.SelectedItem.Text;
                    use.UserKind = txtType.Text.Trim();
                    use.InUse = cbo.Checked ? 1 : 0;
                    if (EnvironmentManagerSystem.BLL.User.WhetherExistence(use))
                    {
                        Jscript.Alert("已存在相同用户名或登录名！", this);
                        return;
                    }
                    if (EnvironmentManagerSystem.BLL.User.AddUser(use))
                    {
                        Response.Write("<script>alert('添加成功');window.parent.art.dialog.list['添加用户'].close();</script>");
                    }
                    else
                    {
                        Jscript.Alert("添加用户失败！", this);
                    }
                }
                catch { }
            }
        }

        /// <summary>
        /// 验证文本框
        /// </summary>
        public Boolean Check()
        {
            if (!txtName.Text.Trim().Equals("")
                && !txtLogName.Text.Trim().Equals("")
                && !txtPwd.Text.Trim().Equals("")
                && !txtType.Text.Trim().Equals(""))
            {
                if (Regex.IsMatch(txtTel.Text.Trim(), @"^((1\d{10})|(1\d{10}[, ，]))+$"))
                {
                    return true;
                }
                else
                {
                    Jscript.Alert("电话号码输入错误", this);
                    return false;
                }
            }
            else
            {
                Jscript.Alert("用户名,密码,登陆名,类型都不能为空", this);
                return false;
            }
        }
    }
}
